import {
  addFlowInfo,
  getApprovalAdminList,
  getApprovalList,
  getFileKey,
  getFlowApprove,
  getFlowInfo,
  stateFlowInfo
} from '@/api/approval'

const state = {
  attendanceList: {},
  id: '',
  state: '',
  backRest: 0,
  baseData: '',
  tableData: [],
  flowData : ""
}

const mutations = {
  SET_ATTENDANCELIST: (state, data) => {
    state.attendanceList = data
  },
  SET_FLOWID: (state, data) => {
    state.id = data
  },
  SET_BACK: (state, data) => {
    if (data) {
      state.backRest = 0
    } else {
      state.backRest++
    }
  },
  SET_FLOWST: (state, data) => {
    state.state = data
  },
  SET_FLOWDATAES: (state, data) => {
    if (data == 'add'){
      state.baseData = ''
      state.tableData = []
      state.flowData = ''
    } else{
      data.baseData ? state.baseData = data.baseData : null
      data.tableData ? state.tableData = data.tableData : null
      data.flowData ? state.flowData = data.flowData : null
    }
  },
}

const actions = {
  // 返回标记
  setBack({ commit }, st){
    commit('SET_BACK', st)
  },
  // 获取审批列表
  getApprovalListAct({ commit }, params) {
    return new Promise((resolve, reject) => {
      getApprovalList(params).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取审批管理列表
  getApprovalAdminListAct({ commit }, params) {
    return new Promise((resolve, reject) => {
      getApprovalAdminList(params).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 审批组列表
  getFlowApproveAct({ commit }, params) {
    return new Promise((resolve, reject) => {
      getFlowApprove(params).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取审批数据
  getFlowInfoAct({ commit }, params) {
    return new Promise((resolve, reject) => {
      getFlowInfo(params).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
// 添加审批流程
  addFlowInfoAct({ commit }, params) {
    return new Promise((resolve, reject) => {
      addFlowInfo(params).then(response => {
        const data = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
// 获取fileKey
getFileKeyAct({ commit }, params){
  return new Promise((resolve, reject) => {
    getFileKey(params).then(response => {
      const { data } = response
      resolve(data)
    }).catch(error => {
      reject(error)
    })
  })
},
// 停用恢复
stateFlowInfoAct({ commit }, params) {
  return new Promise((resolve, reject) => {
    stateFlowInfo(params).then(response => {
      const { data } = response
      resolve(data)
    }).catch(error => {
      reject(error)
    })
  })
},

  // 流程数据处理
  setflowid({ commit }, params) {
    commit('SET_FLOWID', params)
  },
  setflowdata({ commit }, params) {
    commit('SET_FLOWDATAES', params)
  },
  setflowst({ commit }, params) {
    commit('SET_FLOWST', params)
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

